﻿Imports Equipment.DataAccess
Imports Equipment.Business
Imports AppUser = Equipment.DataAccess.User
Imports Equipment.Model

Namespace Equipment
    <RequiresAuthentication()> _
    Public Class ProfileController
        Inherits System.Web.Mvc.Controller

        Function Index() As ActionResult
            Dim profile As AppUser = AppUser.GetByPrimaryKey(Application.CurrentUserInfo.UserID)
            InitControls(profile)
            Return View()
        End Function

        <HttpPost()> _
        Function Index(ByVal collection As FormCollection) As ActionResult

            Dim profile As AppUser = AppUser.GetByPrimaryKey(Application.CurrentUserInfo.UserID)

            If Not String.IsNullOrEmpty(collection("SaveProfile")) Then
                SaveProfile(profile, collection)
            Else
                SaveNotificationPreferences(profile, collection)
            End If

            InitControls(profile)
            Return View()
        End Function

#Region "Private Section"
        Private Sub InitControls(ByVal profile As AppUser)
            Dim timeZones As Dictionary(Of String, String) = SystemHelper.GetTimeZones()

            ViewData("TimeZone") = New SelectList(timeZones, "Key", "Value", profile.Timezone)
            ViewData("profile") = profile
            ViewData("selectedDay") = profile.PreferenceDay
            ViewData("Notifications") = Notification.RetrieveAll()

            For Each userPreference In profile.Preferences
                If userPreference.Immediately Then
                    ViewData(String.Format("selectedImmediately_{0}", userPreference.NotificationID)) = True
                End If
                If userPreference.OnSelectedDay Then
                    ViewData(String.Format("selectedOnSelectedDay_{0}", userPreference.NotificationID)) = True
                End If

            Next

        End Sub

        Private Sub SaveProfile(ByVal profile As AppUser, ByVal collection As FormCollection)

            Dim formPass As String = collection("pass")
            Dim formEmail As String = collection("email")
            Dim formConfirmPass As String = collection("confirmPass")
            Dim formTimeZone As String = collection("TimeZone")

            profile.Password = formPass
            profile.Email = formEmail
            profile.Timezone = formTimeZone

            profile.Save()
        End Sub

        Private Sub SaveNotificationPreferences(ByVal profile As AppUser, ByVal collection As FormCollection)
            Dim selectedDay As Integer = Integer.Parse(collection("rbDay"))
            profile.Preferences.Clear()
            For Each notificationItem In Notification.RetrieveAll()
                Dim userPreference As New Preference()

                Dim immediately As Boolean = Boolean.Parse(collection(String.Format("cbxSendImmediately_{0}", notificationItem.ID)).Split(",")(0))
                Dim onSelectedDays As Boolean = Boolean.Parse(collection(String.Format("cbxSendOnSelectedDays_{0}", notificationItem.ID)).Split(",")(0))

                userPreference.Immediately = immediately
                userPreference.OnSelectedDay = onSelectedDays
                userPreference.UserID = profile.ID
                userPreference.NotificationID = notificationItem.ID
                profile.Preferences.Add(userPreference)
            Next
            profile.SavePreferences()

            profile.PreferenceDay = selectedDay
            profile.Save()

        End Sub

#End Region

    End Class
End Namespace